<?php

use Illuminate\Database\Eloquent\Model;

class dianpurewardModel extends Model
{
    protected $table 		= 'dianpu_reward';
    protected $primaryKey = 'id';
    protected $appends = ['type_text', 'status_name', 'class_text'];

    // 分销佣金状态  table: commission_reward, field: status
    const COMMISSION_REWARD_STATUS_WAITING = 0;  // 未结算、待入账
    const COMMISSION_REWARD_STATUS_ACCOUNTED = 1;  // 已结算、已入账
    const COMMISSION_REWARD_STATUS_BACK = -1;  // 已退回
    const COMMISSION_REWARD_STATUS_CANCEL = -2;  // 已取消

    protected function serializeDate($date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function getStatusNameAttribute($value)
    {
        $status_name = '';

        switch ($this->attributes['status']) {
            case self::COMMISSION_REWARD_STATUS_BACK:
                $status_name = '已退回';
                break;
            case self::COMMISSION_REWARD_STATUS_WAITING:
                $status_name = '待入账';
                break;
            case self::COMMISSION_REWARD_STATUS_ACCOUNTED:
                $status_name = '已入账';
                break;
            case self::COMMISSION_REWARD_STATUS_CANCEL:
                $status_name = '已取消';
                break;
        }

        return $status_name;
    }

    public function getClassTextAttribute($value)
    {
        $class = $value?:$this->attributes['class'];
        $class_text = "未知类型";
        if ($class == 'dianpufenhong') {
            $class_text = '店铺分红';
        }

        return $class_text;
    }

    public function getTypeTextAttribute($value){
        $type = $value?:$this->attributes['type'];
        switch ($type){
            case 'money';
                $type_text = '余额';
                break;

            case 'score';
                $type_text = '积分';
                break;

            case 'cash';
                $type_text = '现金(手动打款)';
                break;

            case 'change';
                $type_text = '企业付款到零钱';
                break;

            case 'bank';
                $type_text = '企业付款到银行卡';
                break;

            default:
                $type_text = '其他方式';
        }

        return $type_text;
    }

    public function dianpuManager(){
        return $this->belongsTo(memberModel::class, 'manager_id', 'id')->select(['id', 'phone', 'nickname', 'realname']);
    }

    public function buyerInfo(){
        return $this->belongsTo(memberModel::class, 'buyer_id', 'id')->select(['id', 'phone', 'nickname', 'realname']);
    }

    public function orderInfo(){
        return $this->belongsTo(orderModel::class, 'order_id', 'id');
    }

    public function orderItemInfo(){
        return $this->belongsTo(orderitemModel::class, 'order_item_id', 'id');
    }

    public function dianpuInfo(){
        return $this->belongsTo(dianpuModel::class, 'dianpu_id', 'id')->select(['company_name', 'phone', 'province_name', 'city_name', 'area_name']);
    }

    /**
     * 待入账
     */
    public function scopeWaiting($query)
    {
        return $query->where('status', self::COMMISSION_REWARD_STATUS_WAITING);
    }

    /**
     * 已退回
     */
    public function scopeBack($query)
    {
        return $query->where('status', self::COMMISSION_REWARD_STATUS_BACK);
    }

    /**
     * 已入账
     */
    public function scopeAccounted($query)
    {
        return $query->where('status', self::COMMISSION_REWARD_STATUS_ACCOUNTED);
    }

    /**
     * 已取消
     */
    public function scopeCancel($query)
    {
        return $query->where('status', self::COMMISSION_REWARD_STATUS_CANCEL);
    }

    /**
     * 构建查询条件
     */
    public static function buildSearch($params)
    {
        $user = \app\service\auth::getUser();

        $start = date('Y-m-d');
        $end = date('Y-m-d', strtotime('+1 day'));
        $date = explode('-', $params['date'] ?? '');
        if (!empty($date) && count($date) == 2) {
            $start = $date[0];
            $end = $date[1] . ' 23:59:59';
        }

        $rewards = self::where('manager_id', $user->id);

        $type = $params['type'] ?? 'all';
        if ($type != 'all' && in_array($type, ['waiting', 'back', 'accounted', 'cancel'])) {
            $rewards = $rewards->{$type}();
        }

        $rewards = $rewards->whereBetween('created_at', [$start, $end]);

        return $rewards;
    }

}